<?php

namespace App\Http\Controllers\Student;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Supervisor;
use App\Title;
use App\Thesis;
use App\Comment;
use App\Description;
use App\Proposal;
use App\Paper;

use Storage;

class ThesisController extends Controller
{

    public function edit_title() {

        $user = \Auth::user();
        $student = $user->student;
        $current = $student->title()->where('current', '>', 0)->first();
        $history = $student->title()->orderBy('created_at', 'desc')->get();
        $edit = false;
        $thesis = $student->thesis;
        if ($thesis == null) {
	    $thesis = new Thesis;
	    $thesis->student_id = $student->id;	
	    $thesis->description = "";
	    $thesis->proposal = "";
	    $thesis->paper = "";

            $thesis->save();;
        } 
        $commentInfo = array("head"=>"和导师讨论选题", "type" => 1, "student_id"=> $student->id);
        $comments = Comment::where('student_id', $student->id)->where(function ($query) {
            $query->where('type', 1)->orWhere('type', 1001);
        })->get();
        return view('student.thesistitle', compact("thesis", "current", "user", "edit", "history", "commentInfo", "comments"));
    }

    public function store_title(Request $request)
    {
        $this->validate($request, [
        'title' => 'required|min:4',
        ]);

        $user = \Auth::user();
        $student = $user->student;
    	$student->title()->update(['current'=>0]);
        $title = new Title;
        $title->title = $request->input('title');
        $title->student_id = $student->id;
        $title->current = 1;
        $title->save();
            $edit = true;
        $thesis = $student->thesis;
        $thesis->approved = false;
        $thesis->save();
        $current = $student->title()->where('current', '>', 0)->first();
        $history = $student->title()->where('current', 0)->orderBy('created_at', 'desc')->get();
        $commentInfo = array("head"=>"和导师讨论选题", "type" => 1, "student_id"=> $student->id);

        $comments = Comment::where('student_id', $student->id)->where(function ($query) {
        	 $query->where('type', 1)->orWhere('type', 1001);
        })->get();

        return view('student.thesistitle', compact("thesis", "current", "user", "edit", "history", "commentInfo", "comments"));

    }


    public function edit_description() {

        $user = \Auth::user();
        $student = $user->student;
        //$current = $student->description()->where('current', '>', 0)->first();
        $history = $student->description()->orderBy('created_at', 'desc')->get();
        $edit = false;
        $thesis = $student->thesis;
        if ($thesis == null) {
            $thesis = new Thesis;
            $thesis->student_id = $student->id;	
            $thesis->description = "";
            $thesis->proposal = "";
            $thesis->paper = "";
            $thesis->save();;
        } 
        $commentInfo = array("head"=>"和导师讨论任务书", "type" => 2, "student_id"=> $student->id);
        $comments = Comment::where('student_id', $student->id)->where(function ($query) {
            $query->where('type', 2)->orWhere('type', 1002);
        })->get();
        return view('student.thesisdescription', compact("thesis", "current", "user", "edit", "history", "commentInfo", "comments"));
    }

    public function store_description(Request $request)
    {
        $this->validate($request, [
        'description' => 'required|min:4',
        'comment' => 'required|min:2',
        ]);
        $user = \Auth::user();
        $student = $user->student;
        $thesis = $student->thesis;

        $count = $student->description->count();
        $file = $request->file('description');

        // 文件是否上传成功
        if ($file->isValid()) {

            // 获取文件相关信息
            $ext = $file->getClientOriginalExtension();     // 扩展名
            $realPath = $file->getRealPath();   //临时文件的绝对路径
            // 上传文件
            $filename = "任务书_" . $user->username . "_" . $user->name . '_'. $count . '.' . $ext;
            // 使用我们新建的uploads本地存储空间（目录）
            if ($thesis->description != null) {
                Storage::disk('descriptions')->delete($thesis->description);
            }
            $bool = Storage::disk('descriptions')->put($filename, file_get_contents($realPath));
            if ($bool) {
                $student->description()->update(['current'=>0]);
                $description = new Description;
                $description->student_id = $student->id;
                $description->description = $filename;
                $description->comment= $request->input('comment');
                $description->current = 1;
                $description->save();
            }

        }
        return redirect()->back()->withInput()->withErrors('提交成功！');
    }


    public function edit_proposal() {

        $user = \Auth::user();
        $student = $user->student;
        $history = $student->proposal()->orderBy('created_at', 'desc')->get();
        $edit = false;
        $thesis = $student->thesis;
        if ($thesis == null) {
            $thesis = new Thesis;
            $thesis->student_id = $student->id; 
            $thesis->description = "";
            $thesis->proposal = "";
            $thesis->paper = "";
            $thesis->save();
        } 
        $commentInfo = array("head"=>"和导师讨论开题报告", "type" => 3, "student_id"=> $student->id);
        $comments = Comment::where('student_id', $student->id)->where(function ($query) {
            $query->where('type', 3)->orWhere('type', 1003);
        })->get();
        return view('student.thesisproposal', compact("thesis", "user", "edit", "history", "commentInfo", "comments"));
    }

    public function store_proposal(Request $request)
    {
        $this->validate($request, [
        'proposal' => 'required|min:4',
        'comment' => 'required|min:2',
        ]);
        $user = \Auth::user();
        $student = $user->student;
        $thesis = $student->thesis;

        $count = $student->proposal->count();
        $file = $request->file('proposal');

        // 文件是否上传成功
        if ($file->isValid()) {

            // 获取文件相关信息
            $ext = $file->getClientOriginalExtension();     // 扩展名
            $realPath = $file->getRealPath();   //临时文件的绝对路径
            // 上传文件
            $filename = "开题报告_" . $user->username . "_" . $user->name . '_'. $count . '.' . $ext;
            // 使用我们新建的uploads本地存储空间（目录）
            if ($thesis->proposal != null) {
                Storage::disk('proposals')->delete($thesis->proposal);
            }
            $bool = Storage::disk('proposals')->put($filename, file_get_contents($realPath));
            if ($bool) {
                $student->proposal()->update(['current'=>0]);
                $proposal = new Proposal;
                $proposal->student_id = $student->id;
                $proposal->proposal = $filename;
                $proposal->comment= $request->input('comment');
                $proposal->current = 1;
                $proposal->save();
            }

        }
        return redirect()->back()->withInput()->withErrors('提交成功！');
    }


    public function edit_paper() {

        $user = \Auth::user();
        $student = $user->student;
        $history = $student->paper()->orderBy('created_at', 'desc')->get();
        $edit = false;
        $thesis = $student->thesis;
        if ($thesis == null) {
            $thesis = new Thesis;
            $thesis->student_id = $student->id; 
            $thesis->description = "";
            $thesis->proposal = "";
            $thesis->paper = "";
            $thesis->save();
        } 
        $commentInfo = array("head"=>"和导师讨论论文", "type" => 4, "student_id"=> $student->id);
        $comments = Comment::where('student_id', $student->id)->where(function ($query) {
            $query->where('type', 4)->orWhere('type', 1004);
        })->get();
        return view('student.thesispaper', compact("thesis", "user", "edit", "history", "commentInfo", "comments"));
    }

    public function store_paper(Request $request)
    {
        $this->validate($request, [
        'paper' => 'required|min:4',
        'comment' => 'required|min:2',
        ]);
        $user = \Auth::user();
        $student = $user->student;
        $thesis = $student->thesis;

        $count = $student->paper->count();
        $file = $request->file('paper');

        // 文件是否上传成功
        if ($file->isValid()) {

            // 获取文件相关信息
            $ext = $file->getClientOriginalExtension();     // 扩展名
            $realPath = $file->getRealPath();   //临时文件的绝对路径
            // 上传文件
            $filename = "论文_" . $user->username . "_" . $user->name . '_'. $count . '.' . $ext;
            // 使用我们新建的uploads本地存储空间（目录）
            if ($thesis->paper != null) {
                Storage::disk('papers')->delete($thesis->paper);
            }
            $bool = Storage::disk('papers')->put($filename, file_get_contents($realPath));
            if ($bool) {
                $student->paper()->update(['current'=>0]);
                $paper = new Paper;
                $paper->student_id = $student->id;
                $paper->paper = $filename;
                $paper->comment= $request->input('comment');
                $paper->current = 1;
                $paper->save();
            }

        }
        return redirect()->back()->withInput()->withErrors('提交成功！');
    }

}
